package com.situ.personal.service;

import java.util.List;
import java.util.Map;

import com.situ.personal.dao.RoleDao;

import com.situ.personal.entity.PageInfo;
import com.situ.personal.entity.Role;

public class RoleService {
	private RoleDao roleDao = new RoleDao();

	public List<Role> findAll() {
		
		return roleDao.findAll();
	}

	public boolean update(Role role) {
		int count = roleDao.update(role);
		if (count == 1) {
			return true;
		} else {
			return false;
		}
	}

	public Role selectById(int id) {
		return roleDao.selectById(id);
	}

	public boolean insert(Role role) {
		int count = roleDao.insert(role);
		if (count == 1) {
			return true;
		} else {
			return false;
		}
	}

	public boolean deleteById(int id) {
		int count = roleDao.deleteById(id);
		if (count == 1) {
			return true;
		} else {
			return false;
		}
	}

	public PageInfo findByPage(int pageNo, int pageSize) {
		PageInfo pageInfo = new PageInfo();
		pageInfo.setPageNo(pageNo);
		pageInfo.setPageSize(pageSize);
		
		// totalPage 
		int count = roleDao.getTotalCount();
		System.out.println(count);
		int totalPage = (int)Math.ceil((double)count / pageSize);
		pageInfo.setTotalPage(totalPage);
		System.out.println(totalPage);
		
		// list
		// pageNo,pageSize-->limit (pageNo-1)*pageSize,pageSize
		int offset = (pageNo - 1) * pageSize;
		//List<User> list = departmentDao.findByPage(offset, pageSize);
		List<Map<String, Object>> list = roleDao.findByPage(offset, pageSize);
		pageInfo.setList(list);
		
		return pageInfo;
	}

}
